package com.lgx.mobile.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lgx.common.exception.DataException;
import com.lgx.common.web.Pagination;
import com.lgx.mobile.dao.IGroupDao;
import com.lgx.mobile.entity.Group;
import com.lgx.mobile.service.IGroupService;
import com.lgx.mobile.vo.GroupVO;
import com.lgx.right.dao.IUserDao;
import com.lgx.right.entity.User;

/**
 * 圈子Service
 * 
 * @author xuyichen
 *
 */
@Service("groupService")
public class GroupService implements IGroupService {

	@Autowired
	private IGroupDao groupDao;

	public IGroupDao getGroupDao() {
		return groupDao;
	}

	public void setGroupDao(IGroupDao groupDao) {
		this.groupDao = groupDao;
	}
	
	@Autowired
	private IUserDao userDao;

	public IUserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	@Transactional
	public Group createGroup(Group group) throws DataException {
		return this.groupDao.createGroup(group);
	}

	@Transactional
	public Group updateGroup(Group group) throws DataException {
		return this.groupDao.updateGroup(group);
	}

	@Transactional
	public void deleteGroup(Group group) throws DataException {
		this.groupDao.deleteGroup(group);
	}

	@Transactional
	public void deleteGroupById(long id) throws DataException {
		this.groupDao.deleteGroupById(id);
	}

	public Group findGroupById(long id) {
		return this.groupDao.findGroupById(id);
	}
	
	public Group checkGroupExists(long createUserId,Date createDate,long categoryId){
		return this.getGroupDao().checkGroupExists(createUserId,createDate,categoryId);
	}

	public List<Group> findGroupList() {
		return this.groupDao.findGroupList();
	}
	
	public List<GroupVO> findHotGroupList(long num){
		return this.groupDao.findHotGroupList(num);
	}

	@Override
	public Pagination findGroupPagination(Object[] params, Class<?> clazz, int pageSize, int pageNum) {
		return this.groupDao.findGroupPagination(params, clazz, pageSize, pageNum);
	}

	@Override
	public Pagination findMyGroupPagination(long userId, Object[] params, Class<?> clazz, int pageSize, int pageNum) {
		return this.groupDao.findMyGroupPagination(userId, params, clazz, pageSize, pageNum);
	}

	@Override
	public GroupVO findGroupVOByGroupId(long greoupId) {
		GroupVO groupVO = new GroupVO();
		Group group = this.findGroupById(greoupId);
		if(group != null){
			groupVO.setId(group.getId());
			groupVO.setName(group.getName());
			groupVO.setDescription(group.getDescription());
			groupVO.setCreateDate(group.getCreateDate());
			groupVO.setCategoryId(group.getCategoryId());
			groupVO.setGroupImage(group.getGroupImage());
			groupVO.setCreateUserId(group.getCreateUserId());
			groupVO.setUserNum(group.getUsers().size());
			User createUser = this.userDao.findUserById(group.getCreateUserId());
			if(createUser != null){
				groupVO.setCreateUserNickName(createUser.getNickname());
				groupVO.setCreateUserName(createUser.getUsername());
				groupVO.setCreateUserTruename(createUser.getTruename());
				groupVO.setCreateUserEmail(createUser.getEmail());
				groupVO.setCreateUserSex(createUser.getSex());
				groupVO.setCreateUserMobile(createUser.getMobile());
				groupVO.setCreateUserCardId(createUser.getCard().getId());
			}else{
				groupVO.setCreateUserNickName("");
				groupVO.setCreateUserName("");
				groupVO.setCreateUserTruename("");
				groupVO.setCreateUserEmail("");
				groupVO.setCreateUserSex(0);
				groupVO.setCreateUserMobile("");
				groupVO.setCreateUserCardId("");
			}
			return groupVO;
		}
		return null;
	}
}
